package com.spingbootes.kizuki.controller;

import com.spingbootes.kizuki.pojo.ApexInfo;
import com.spingbootes.kizuki.service.ESService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/es")
public class ESController {

    @Autowired
    private ESService esService;

    @GetMapping("/insert")
    public String insert(@RequestBody ApexInfo info) {
        esService.insert(info);
        return info.toString();
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable String id) {
        esService.delete(id);
        return String.valueOf(id);
    }

    @GetMapping("/update")
    public String update(@RequestBody ApexInfo info) {
        esService.update(info);
        return info.toString();
    }

    @GetMapping("/select/{id}")
    public String select(@PathVariable String id) {
        ApexInfo info = esService.select(id);
        return Optional.ofNullable(info).map(ApexInfo::toString).orElse("null");
    }

    @GetMapping("/select")
    public String selectAll() {
        List<ApexInfo> apexInfos = esService.selectAll();
        return apexInfos.toString();
    }

    @GetMapping("/select-detail/{id}")
    public String selectDetail(@PathVariable String id) {
        List<ApexInfo> apexInfos = esService.selectDetail(id);
        return apexInfos.toString();
    }

}
